package com.budwk.app.sqfwV2.generator.weiqingmen.survey;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BaseExcelGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelDataInto;
import com.budwk.app.sqfwV2.dto.weiqingmen.HouseHoldSurveyDTO;
import com.budwk.app.sqfwV2.dto.weiqingmen.HouseHoldSurveyExcelImageConfigBO;
import com.budwk.app.sqfwV2.dto.weiqingmen.InterviewDTO;
import com.budwk.app.sqfwV2.enums.weiqingmen.HouseHoldSurveyEnum;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-08-02
 * <p>
 * 威清路入户调查 HouseHoldSurvey
 * 九类人员巡查台账.xlsx
 */
public class NinePersonnelExcelGenerator extends BaseExcelGenerator {

    /**
     * 自定义excel的表头数据
     *
     * @param excelParam         excel参数整体参数map
     * @param excelGenerateParam 创建excel所提供的参数
     */
    @Override
    protected void customerPopularHeaderData(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    /**
     * 后端自定义维护表头数据
     *
     * @param excelParam         excel整体参数
     * @param excelGenerateParam 创建excel所提供的参数
     */
    @Override
    protected void customerPopularHeaderDataForAdmin(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    /**
     * @param excelParam         excel整体参数
     * @param excelGenerateParam 创建excel所提供的参数
     * @return 判断excel是否含有数据
     */
    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeErgodic(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        // 如果不是数据检测，直接返回
        if (excelGenerateParam.getExcelConfig().getCreateExcelFileFlag()) return null;

        // 数据检测
        List<ExcelRowDataParam> dataList = excelGenerateParam.getDataList();
        ResultOfExcelDataInto resultOfExcelDataInto = new ResultOfExcelDataInto();
        resultOfExcelDataInto.setExcelHaveDataFlag(false);
        for (ExcelRowDataParam excelRowDataParam : dataList) {
            // 判断是否有满足条件的数据，只有检测到一条就可以结束
            if (addRowDataCondition(excelRowDataParam, excelGenerateParam)) {
                resultOfExcelDataInto.setExcelHaveDataFlag(true);
                break;
            }
        }
        return resultOfExcelDataInto;
    }

    /**
     * 根据行数据判断是否该条数据进入excel中
     *
     * @param rowDataParam       行数据
     * @param excelGenerateParam excel生成所提供的参数
     * @return 是/否
     */
    @Override
    protected boolean addRowDataCondition(ExcelRowDataParam rowDataParam, ExcelGenerateParam excelGenerateParam) {
        HouseHoldSurveyDTO fieldDTO = (HouseHoldSurveyDTO) rowDataParam.getAbstractCustomerPdfCreateField();
        List<String> careCategory = fieldDTO.getCareCategory();

        boolean createFlag = false;
        if (!careCategory.contains("正常")) {
            createFlag = true;

        }
        return createFlag;

    }

    /**
     * 用户自定义填充excel实体数据
     *
     * @param listMap            数据数据
     * @param excelGenerateParam 创建excel所需的参数
     */
    @Override
    protected void customerPopularSheetData(List<Map<String, Object>> listMap, ExcelGenerateParam excelGenerateParam) {

    }

    /**
     * 自定义填充excel中每一行的数据
     *
     * @param rowDataMap   当前行的数据Map
     * @param rowDataParam 所提供的当前行的数据
     */
    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {
        HouseHoldSurveyDTO fieldDTO = (HouseHoldSurveyDTO) rowDataParam.getAbstractCustomerPdfCreateField();
        if ("否".equals(fieldDTO.getHomeDanger())) {
            rowDataMap.put("homeDanger", "未发现问题");
        } else {
            rowDataMap.put("homeDanger", fieldDTO.getHomeDangerInfo());
        }

        List<String> careCategory = fieldDTO.getCareCategory();
        StringBuilder stringBuilder = new StringBuilder();
        for (String v : careCategory) {
            if (stringBuilder.length() == 0) {
                stringBuilder.append(v);
            } else {
                stringBuilder.append("、").append(v);
            }
        }
        rowDataMap.put("careCategory", stringBuilder.toString());

        CompleteFlowBusinessParam param = (CompleteFlowBusinessParam) rowDataParam.getBusinessParam();
        Sqfw_formSummary_new formSummary = param.getFormSummary();
        rowDataMap.put("community", formSummary.getCommunity());

        StringBuilder fillDate = new StringBuilder();
        List<InterviewDTO> interviewDTOList = fieldDTO.getInterviewDTOList();
        interviewDTOList.sort(Comparator.comparing(InterviewDTO::getFillDate)); // 按时间升序
        for (InterviewDTO interviewDTO : interviewDTOList) {
            if (fillDate.length() == 0) {
                fillDate.append(DateUtil.format(interviewDTO.getFillDate(), DatePattern.NORM_DATE_PATTERN));
            } else {
                fillDate.append("、").append(DateUtil.format(interviewDTO.getFillDate(), DatePattern.NORM_DATE_PATTERN));
            }
        }
        rowDataMap.put("fillDate", fillDate.toString());
    }

    /**
     * 后端生成excel时候自定义填充excel中每一行的数据
     *
     * @param rowDataMap   当前行的数据Map
     * @param rowDataParam 所提供的当前行的数据
     */
    @Override
    protected void customerPopularRowDataMapForAdmin(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {
    }

    /**
     * 在创建之前判断excel中是否含有数据
     *
     * @param excelParam         创建之前excel中已经填充的数据
     * @param excelGenerateParam 所提供的excel创建的所需要的数据
     * @return 结果
     */
    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeCreate(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected String getTemplatePath(ExcelGenerateParam excelGenerateParam) {
        return "sqfwV2/weiqingmen/survey/NinePersonnel.xlsx";
    }

    /**
     * 可以参照 {@link com.budwk.app.sqfw.enums.DownLoadFileNameEnum}
     *
     * @return 文件名名称
     */
    @Override
    public String getDownloadFileName(ExcelGenerateParam excelGenerateParam) {
        //TODO 在这里返回你生成的文件名名称
        // 注意注意注意不用带上后缀
        return HouseHoldSurveyEnum.NINE_PERSONNEL.getOneName();
    }


    @Override
    protected Class<?> getImageConfigBO() {
        return HouseHoldSurveyExcelImageConfigBO.class;
    }

    /**
     * 允许用户在创建excel文件之前对配置进行修改
     *
     * @param excelFileCreateParamBO 创建excel的配置参数
     */
    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParamBO) {

    }
}
